package com.zmzncs.lmtc.module.promotion.controller;


import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.CustomizeRuleStatusEnum;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.MonthRentTypeEnum;
import com.zmzncs.lmtc.common.util.EnumUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.promotion.pojo.customize_rule.CustomizeRule;
import com.zmzncs.lmtc.module.promotion.pojo.customize_rule.CustomizeRuleVO;
import com.zmzncs.lmtc.module.promotion.pojo.customize_rule_fee_scale.CustomizeRuleFeeScale;
import com.zmzncs.lmtc.module.promotion.pojo.customize_rule_fee_scale.CustomizeRuleFeeScaleMobileVO;
import com.zmzncs.lmtc.module.promotion.service.CustomizeRuleFeeScaleService;
import com.zmzncs.lmtc.module.promotion.service.CustomizeRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-08-13
 */
@Api(tags = "定制规则")
@RestController
@RequestMapping("/promotion/customize-rule")
@Validated
public class CustomizeRuleController {

    @Autowired
    CustomizeRuleService customizeRuleService;
    @Autowired
    CustomizeRuleFeeScaleService customizeRuleFeeScaleService;


    @ApiOperation(value = "查询定制规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "getCustomizeRule")
    public Rest<CustomizeRuleVO> getCustomizeRule() {
        //  查询规则
        List<CustomizeRule> customizeRuleList = customizeRuleService.list(null);
        //  查询规则收费标准
        List<CustomizeRuleFeeScale> customizeRuleFeeScaleList = customizeRuleFeeScaleService.list(null);

        CustomizeRuleVO customizeRuleVO = new CustomizeRuleVO();
        for(CustomizeRule customizeRule: customizeRuleList){
            //  2个停车场
            if(customizeRule.getParkNum().equals(2)){
                //  停车场状态
                CustomizeRuleStatusEnum customizeRuleStatusEnum = EnumUtil.getByCode(customizeRule.getStatus(), CustomizeRuleStatusEnum.class);
                customizeRuleVO.setTwoParkStatus(customizeRuleStatusEnum);

                //  停车场折率
                for(CustomizeRuleFeeScale customizeRuleFeeScale : customizeRuleFeeScaleList) {
                    if(customizeRuleFeeScale.getCustomizeRuleId().equals(customizeRule.getId())){
                        //  月付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.MONTH.getCode()){
                            customizeRuleVO.setTwoParkMonthDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  季付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.QUARTER.getCode()){
                            customizeRuleVO.setTwoParkQuarterDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  年付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.YEAR.getCode()){
                            customizeRuleVO.setTwoParkYearDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                    }
                }
            }

            //  3个停车场
            if(customizeRule.getParkNum().equals(3)){
                //  停车场状态
                CustomizeRuleStatusEnum customizeRuleStatusEnum = EnumUtil.getByCode(customizeRule.getStatus(), CustomizeRuleStatusEnum.class);
                customizeRuleVO.setThreeParkStatus(customizeRuleStatusEnum);

                //  停车场折率
                for(CustomizeRuleFeeScale customizeRuleFeeScale : customizeRuleFeeScaleList) {
                    if(customizeRuleFeeScale.getCustomizeRuleId().equals(customizeRule.getId())){
                        //  月付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.MONTH.getCode()){
                            customizeRuleVO.setThreeParkMonthDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  季付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.QUARTER.getCode()){
                            customizeRuleVO.setThreeParkQuarterDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  年付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.YEAR.getCode()){
                            customizeRuleVO.setThreeParkYearDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                    }
                }
            }

            //  4个停车场
            if(customizeRule.getParkNum().equals(4)){
                //  停车场状态
                CustomizeRuleStatusEnum customizeRuleStatusEnum = EnumUtil.getByCode(customizeRule.getStatus(), CustomizeRuleStatusEnum.class);
                customizeRuleVO.setFourParkStatus(customizeRuleStatusEnum);

                //  停车场折率
                for(CustomizeRuleFeeScale customizeRuleFeeScale : customizeRuleFeeScaleList) {
                    if(customizeRuleFeeScale.getCustomizeRuleId().equals(customizeRule.getId())){
                        //  月付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.MONTH.getCode()){
                            customizeRuleVO.setFourParkMonthDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  季付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.QUARTER.getCode()){
                            customizeRuleVO.setFourParkQuarterDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  年付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.YEAR.getCode()){
                            customizeRuleVO.setFourParkYearDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                    }
                }
            }

            //  5个停车场
            if(customizeRule.getParkNum().equals(5)){
                //  停车场状态
                CustomizeRuleStatusEnum customizeRuleStatusEnum = EnumUtil.getByCode(customizeRule.getStatus(), CustomizeRuleStatusEnum.class);
                customizeRuleVO.setFiveParkStatus(customizeRuleStatusEnum);

                //  停车场折率
                for(CustomizeRuleFeeScale customizeRuleFeeScale : customizeRuleFeeScaleList) {
                    if(customizeRuleFeeScale.getCustomizeRuleId().equals(customizeRule.getId())){
                        //  月付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.MONTH.getCode()){
                            customizeRuleVO.setFiveParkMonthDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  季付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.QUARTER.getCode()){
                            customizeRuleVO.setFiveParkQuarterDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                        //  年付折率
                        if(customizeRuleFeeScale.getMonthRentType().intValue() == MonthRentTypeEnum.YEAR.getCode()){
                            customizeRuleVO.setFiveParkYearDiscountRate(customizeRuleFeeScale.getDiscountRate());
                        }
                    }
                }
            }
        }

        return RestUtil.success(customizeRuleVO);
    }

    @ApiOperation(value = "计算价格")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="parkIds", value="停车场id集合", allowMultiple = true, required = true),
    })
    @PostMapping(value = "computePrice")
    public Rest<CustomizeRuleFeeScaleMobileVO> computePrice(@RequestParam(value = "parkIds") @Size(min = 2, max = 5) List<Long> parkIds) {
        CustomizeRuleFeeScaleMobileVO customizeRuleFeeScaleMobileVO = customizeRuleService.computePrice(parkIds);

        return RestUtil.success(customizeRuleFeeScaleMobileVO);
    }

}

